﻿using One.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OneWeb.Repository
{
    public class ContentRepository : Repository<Content>
    {
        public Content GetToday()
        {
            Content content = DbSet.OrderByDescending(x => x.PostDate).FirstOrDefault();
            return content ?? new Content() ;
        }

        public Content GetPrevious()
        {
            Content currentContent = DbSet.OrderByDescending(x => x.PostDate).FirstOrDefault();
            Content content = null;
            if (currentContent != null)
            {
                content = (from c in DbSet
                                   where c.PostDate < currentContent.PostDate
                                   orderby c.PostDate descending
                                   select c).FirstOrDefault();
            }
            return content;
        }

        public Content GetNext()
        {
            Content currentContent = DbSet.OrderByDescending(x => x.PostDate).FirstOrDefault();
            Content content = null;
            if (currentContent != null)
            {
                content = (from c in DbSet
                           where c.PostDate > currentContent.PostDate
                           orderby c.PostDate ascending
                           select c).FirstOrDefault();
            }
            return content;
        }

        public Content GetToday(int ?id)
        {
            Content content = DbSet.Find(id);
            return content ?? new Content();
        }

        public Content GetPrevious(int currentId)
        {
            Content currentContent = DbSet.Find(currentId);
            Content content = null;
            if (currentContent != null)
            {
                content = (from c in DbSet
                           where c.PostDate < currentContent.PostDate
                           orderby c.PostDate descending
                           select c).FirstOrDefault();
            }
            return content;
        }

        public Content GetNext(int currentId)
        {
            Content currentContent = DbSet.Find(currentId);
            Content content = null;
            if (currentContent != null)
            {
                content = (from c in DbSet
                           where c.PostDate > currentContent.PostDate
                           orderby c.PostDate ascending
                           select c).FirstOrDefault();
            }
            return content;
        }
    }
}